package com.clock.shop.service.impl;

import com.clock.shop.domain.EmployeeInfo;
import com.clock.shop.domain.ShopEmployee;
import com.clock.shop.domain.ShopOrder;
import com.clock.shop.mapper.ShopEmployeeMapper;
import com.clock.shop.mapper.ShopOrderMapper;
import com.clock.shop.service.IShopClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Service
public class ShopClockServiceImpl implements IShopClockService {

    @Autowired
    private ShopEmployeeMapper shopEmployeeMapper;

    @Autowired
    private ShopOrderMapper shopOrderMapper;

    //存员工信息的链表
    LinkedList<EmployeeInfo> employeeInfos = new LinkedList<>();

    //更新员工顺序之后更新排序
    @Override
    public LinkedList<EmployeeInfo> updateLine(List<Long> employeeIds) {
        for (Long id : employeeIds) {
            ShopEmployee shopEmployee = shopEmployeeMapper.selectShopEmployeeByEmployeeId(id);//先获取店员的信息
            Integer status = shopEmployee.getEmployeeStatus();//再获取店员的状态
            EmployeeInfo employeeInfo = new EmployeeInfo();//新建一个EmployeeInfo（装员工信息）的对象
            employeeInfo.setEmoloyeeId(id); // 设置员工ID
            employeeInfo.setEmployeeStatus(status); // 设置员工状态
            employeeInfos.add(employeeInfo); // 将创建的EmployeeInfo对象添加到链表中
        }

        return employeeInfos;
    }

    //排序-员工完成订单之后
    @Override
    public LinkedList<EmployeeInfo> sortEmployee(Long OverId, int clock) {
        //根据员工id查找订单（且订单状态为进行中）
        ShopOrder shopOrder = shopOrderMapper.selectShopOrderByEmployeeIdIng(OverId);

        if (clock == 0) {
            roundClock(OverId);
        } else if (clock == 1) {
            //根据订单开始时间和结束时间时间差计算服务时常,单位为分钟
            long diffInMillis = TimeUnit.MILLISECONDS.toMinutes(shopOrder.getOrderOver().getTime() - shopOrder.getOrderStart().getTime());

            //判断服务时常是否大于三十分钟，大于三十分钟轮牌，小于三十分钟轮钟
            if(diffInMillis > 30) {
                roundLine(OverId);
            } else {
                roundClock(OverId);
            }
        }

//        //根据订单开始时间和结束时间时间差计算服务时常,单位为分钟
//        long diffInMillis = TimeUnit.MILLISECONDS.toMinutes(shopOrder.getOrderOver().getTime() - shopOrder.getOrderStart().getTime());
//
//        //判断服务时常是否大于三十分钟，大于三十分钟轮牌，小于三十分钟轮钟
//        if(diffInMillis > 30) {
//            roundLine(OverId);
//        } else {
//            roundClock(OverId);
//        }

        //设置订单状态为已完成
        shopOrder.setOrderStatus("2");
        shopOrderMapper.updateShopOrder(shopOrder);
        return employeeInfos;
    }

    //轮钟
    @Override
    public void roundClock(Long OverId) {
        //顺序不变,链表不动,将该员工状态设置为空闲
        for (EmployeeInfo employee : employeeInfos) {
            if (employee.getEmoloyeeId().equals(OverId)) {
                employee.setEmployeeStatus(0);
                break;
            }
        }
    }
    //轮排
    @Override
    public void roundLine(Long OverId) {
        //该员工服务完成后排在最后面
        for (EmployeeInfo employee : employeeInfos) {
            if (employee.getEmoloyeeId().equals(OverId)) {
                employeeInfos.remove(employee);//将员工从当前位置移除
                employeeInfos.add(employee);//将员工添加回链表，此时他在链表的末尾
                employee.setEmployeeStatus(0);
                break;
            }
        }
    }


}
